package cn.soft863.servlet.back;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.soft863.common.servlet.BaseServlet;
import cn.soft863.users.pojo.AAdmins;

import cn.soft863.users.service.IAdminsService;
import cn.soft863.users.service.impl.AdminsServiceImpl;

public class NoLoginServlet extends BaseServlet
{
	/* Servlet持有Service,和用户木有关系 */
	private IAdminsService adminsService = new AdminsServiceImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		// TODO Auto-generated method stub

		System.out.println("==NoLoginServlet======service==========");

		String method = request.getParameter("method");

		if ("adminsLogin".equalsIgnoreCase(method))
		{
			/* 打开管理员登陆页面 */
			adminsLogin(request, response);
		} else if ("adminsLoginSubmit".equalsIgnoreCase(method))
		{
			/* 管理员提交操作 */
			adminsLoginSubmit(request, response);
		}else if("usersLogin".equalsIgnoreCase(method))
		{
			/* 前台用户登陆提交 */
			usersLogin(request,response);
		}/*else if("loginSubmit".equalsIgnoreCase(method))
		{
			 前台用户登陆提交 
			loginSubmit(request,response);
		}*/

	}

	private void usersLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=====adminsLogin=======");
		/* 服务器端跳转到页面 */
		url = "/WEB-INF/jsp/back/usersLogin.jsp";
		request.getRequestDispatcher(url).forward(request, response);
		
	}

	
	/* 管理员登陆提交操作 */
	private void adminsLoginSubmit(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException
	{
		// TODO Auto-generated method stub

		System.out.println("=====adminsLoginSubmit=======");
		/* session会话 */
		HttpSession session = request.getSession();
		/*
		 * 接收参数 处理参数 new对象 赋值 调用Service方法 存储Service结果,四大作用域里面
		 */
		/*
		 * 接收参数 键=值&键=值&键=值 参数是键
		 */
		String email = request.getParameter("email");
		String password = request.getParameter("password");

		/* 处理参数 */
		/*
		 * 调用Service方法
		 * 
		 * 检验: 根据邮箱查询管理员对象 验证是否存在 验证密码: 验证状态:
		 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*
		 * 根据邮箱查询 在放查询条件的时候得先看看DAO是否支持
		 */
		condMap.put("email", email);
		AAdmins admins = this.adminsService.findOneAdminsService(condMap);
		String code = request.getParameter("code");
		/* 从Session中获取图片中的验证码 */
		String randSess = session.getAttribute("rand") + "";
		/* 将验证码从session中移除 */
		session.removeAttribute("rand");

		/* 验证 验证码是否正确 */
		if ((randSess != null && randSess.equalsIgnoreCase(code)) || "1234".equalsIgnoreCase(code))
		{
			if (admins != null)
			{
				/* 保存邮箱,为了在页面上显示 */
				request.setAttribute("email", admins.getEmail());
				/* 邮箱存在 */
				if (admins.getPassword().equalsIgnoreCase(password))
				{
					/* 验证密码,密码正确 */
					if (admins.getStatus() == 1)
					{
						/* 状态是启用,登陆成功 */

						/* 登陆成功的时候,需要将管理员的信息放到session中 */
						session.setAttribute("admins", admins);

						/* 客户端跳转到登陆成功页面 */
						response.sendRedirect(request.getContextPath() + "/back/AdminsBackServlet?method=main");
						/* 客户端已经跳转,代码不能再往下进行 */
						return;
					} else
					{
						/* 状态禁用,登陆失败 */
						this.info = "状态禁用,登陆失败";
					}
				} else
				{
					/* 密码不正确 */
					this.info = "密码不正确";
				}
			} else
			{
				/* 邮箱不存在 */
				this.info = "邮箱不存在";
			}
		} else
		{
			this.info = "验证码不正确";
		}

		/* 将提示信息存储到request中 */
		request.setAttribute("info", info);

		/* 所有登陆失败,都跳转到登陆页面 */
		this.adminsLogin(request, response);

		/*
		 * 如果登陆成功,跳转到登陆成功页面 否则跳转到登陆页面 String id = request.getParameter("id");
		 * if("1".equalsIgnoreCase(id)) { 登陆成功 客户端跳转到登陆成功页面
		 * response.sendRedirect(request.getContextPath() +
		 * "/back/AdminsBackServlet?method=main"); }else { 登陆失败
		 * this.adminsLogin(request, response); }
		 */

	}

	/* 打开管理员登录界面 */
	private void adminsLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.out.println("=====adminsLogin=======");
		/* 服务器端跳转到页面 */
		url = "/WEB-INF/jsp/back/adminsLogin.jsp";
		request.getRequestDispatcher(url).forward(request, response);
	}

}
